package test;

import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;

import org.apache.oltu.oauth2.client.OAuthClient;
import org.apache.oltu.oauth2.client.URLConnectionClient;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.client.response.GitHubTokenResponse;
import org.apache.oltu.oauth2.common.OAuthProviderType;
import org.apache.oltu.oauth2.common.error.OAuthError.TokenResponse;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.types.GrantType;
import org.scribe.builder.ServiceBuilder;
import org.scribe.model.OAuthConfig;
import org.scribe.model.SignatureType;
import org.scribe.model.Token;
import org.scribe.oauth.OAuthService;

import apigoogle.*;

public class GoogleDriverTest2 {

	public static void main(String[] args) throws OAuthSystemException,
			IOException {
		String ClientID = "45339683173.apps.googleusercontent.com";
		String Clientsecret = "Gle2rG7-qPKy-uq_czkvZZTP";
		String callback = "http://localhost:8080/login.html";
		String SCOPE = "https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/drive.install https://www.googleapis.com/auth/drive.appdata";

		OutputStream stream = new DataOutputStream(new FileOutputStream(
				"test1.xml"));

		OAuthConfig config = new OAuthConfig(ClientID, Clientsecret, callback,
				SignatureType.QueryString, SCOPE, stream);

		GoogleAPi api = new GoogleAPi();

		OAuthService service = api.createService(config);

		String u = service.getAuthorizationUrl(null);

		System.out.println(u);
		Desktop.getDesktop().browse(
				URI.create(service.getAuthorizationUrl(null)));
		System.out
				.println("Please open the following URL in your browser then type the authorization code:");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String access_token = br.readLine();

		System.out.println(access_token);

		URL url = new URL(
				"https://www.googleapis.com/oauth2/v1/userinfo?access_token="
						+ access_token);

		HttpURLConnection con = (HttpURLConnection) url.openConnection();
		int serverCode = con.getResponseCode();
		System.out.println(serverCode);

		InputStream in = con.getInputStream();
		OutputStream os = new DataOutputStream(new FileOutputStream("test.xml"));
		final int length = 5000;
		byte[] bytes = new byte[length];
		int bytesRead = 0;
		while ((bytesRead = in.read(bytes, 0, length)) > 0) {
			os.write(bytes, 0, bytesRead);
			os.flush();
		}
		os.close();
		
		
		URL url1 = new URL(
				"https://www.googleapis.com/drive/v2/files?q='root in parents'");

		HttpURLConnection con1 = (HttpURLConnection) url1.openConnection();
		int serverCode1 = con1.getResponseCode();
		System.out.println(serverCode1);

		InputStream in1 = con.getInputStream();
		OutputStream os1 = new DataOutputStream(new FileOutputStream("test.xml"));
		final int length1 = 5000;
		byte[] bytes1 = new byte[length1];
		int bytesRead1 = 0;
		while ((bytesRead = in1.read(bytes1, 0, length1)) > 0) {
			os1.write(bytes1, 0, bytesRead1);
			os1.flush();
		}
		os1.close();
		
		
		
		
		
		// try{
		// OAuthClientRequest request = OAuthClientRequest
		// .authorizationProvider(OAuthProviderType.GOOGLE)
		// .setClientId(ClientID).setRedirectURI(callback)
		// .buildQueryMessage();
		//
		// // in web application you make redirection to uri:
		// System.out.println("Visit: " + request.getLocationUri()
		// + "\nand grant permission");
		//
		// System.out
		// .print("Now enter the OAuth code you have received in redirect uri ");
		// BufferedReader br = new BufferedReader(new
		// InputStreamReader(System.in));
		// String code = br.readLine();
		//
		// request = OAuthClientRequest.tokenProvider(GoogleAPi.class)
		// .setGrantType(GrantType.AUTHORIZATION_CODE)
		// .setClientId(ClientID)
		// .setClientSecret(Clientsecret)
		// .setRedirectURI(callback).setCode(code)
		// .buildBodyMessage();
		//
		// OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
		//
		// //Facebook is not fully compatible with OAuth 2.0 draft 10, access
		// token response is
		// //application/x-www-form-urlencoded, not json encoded so we use
		// dedicated response class for that
		// //Own response class is an easy way to deal with oauth providers that
		// introduce modifications to
		// //OAuth specification
		// GitHubTokenResponse oAuthResponse = oAuthClient.accessToken(request,
		// GitHubTokenResponse.class);
		//
		// System.out.println(
		// "Access Token: " + oAuthResponse.getAccessToken() + ", Expires in: "
		// + oAuthResponse
		// .getExpiresIn());
		// } catch(OAuthProblemException e) {
		// System.out.println("OAuth error: " + e.getError());
		// System.out.println("OAuth error description: " + e.getDescription());
		// }
		// }
	}
}
